package services

import (
	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
	"mygo/app/models"
	"mygo/app/tools"
	"strings"
)

var regionMap = map[string]string{
	"cn": "苍南近岸海域",
	"py": "平阳近岸海域",
	"ra": "瑞安近岸海域",
	"sa": "三澳海域",
	"qt": "浅滩工程海域",
	"lw": "龙湾近岸海域",
	"lg": "龙港近岸海域",
	"lq": "乐清近岸海域",
}

var regionMapTwc = map[string]string{
	"cn": "苍南",
	"py": "平阳",
	"ra": "瑞安",
	"sa": "三澳",
	"qt": "浅滩",
	"lw": "龙湾",
	"lg": "龙港",
	"lq": "乐清",
}

var regionCnMap = map[string]string{
	"cn": "苍南",
	"py": "平阳",
	"ra": "瑞安",
	"sa": "三澳核电厂海域",
	"qt": "浅滩",
	"lw": "龙湾",
	"lg": "龙港",
	"lq": "乐清",
}

var sk = []string{"白天", "夜间", "早晨", "上午", "中午", "下午", "傍晚", "上半夜", "半夜", "下半夜"}

// services.HandleWave(&alarmDetail, region, ctl.db)
func HandleWave(alarmDetail *models.AlarmDetail, region string, db *gorm.DB, c *gin.Context) {
	str := alarmDetail.Data
	//去除字符串中的空格
	str = strings.Replace(str, " ", "", -1)
	//根据换行符分割
	strArr := strings.Split(str, "\n")
	//删除strArr中长度为0的元素
	for i := 0; i < len(strArr); i++ {
		if len(strArr[i]) == 0 {
			strArr = append(strArr[:i], strArr[i+1:]...)
			i--
		}
	}
	var arr []string
	for i := 0; i < len(strArr); i++ {
		if strings.Contains(strArr[i], "根据") {
			arr = append(arr, strArr[i])
		}
		if strings.Contains(strArr[i], "未来24小时") {
			if region == "cn" || region == "py" || region == "ra" {
				strArr[i] = strings.Split(strArr[i], "，")[0] + "，" + regionMap[region] + "未来24小时海浪预报："
			} else {
				strArr[i] = strings.Split(strArr[i], "，")[0] + "，" + "未来24小时海浪预报："
			}
			arr = append(arr, strArr[i])
		}
		if region == "cn" || region == "py" || region == "ra" {
			if strings.Contains(strArr[i], "温州近岸") {
				strArr[i] = strings.Split(strArr[i], "温州近岸海域")[1]
				strArr[i] = tools.DelColor(strArr[i])
				arr = append(arr, strings.ReplaceAll(strArr[i], "；", "。"))
			}
			if strings.Contains(strArr[i], "请") {
				strArr[i] = strings.Replace(strArr[i], "我台的", "", -1)
				arr = append(arr, strArr[i])
			}
		} else if region == "sa" {
			if strings.Contains(strArr[i], "温州近岸") {
				strArr[i] = tools.DelColor(strArr[i])
				numArr := tools.GetNumFloat64(strArr[i])
				if len(numArr) == 4 {
					//if numArr[1] > numArr[3] {
					//	numArr[0], numArr[1], numArr[2], numArr[3] = numArr[2], numArr[3], numArr[0], numArr[1]
					//}
					//深度拷贝numArr
					numArrCopy := make([]float64, len(numArr))
					copy(numArrCopy, numArr)
					if numArrCopy[1] > numArrCopy[3] {
						numArrCopy[0], numArrCopy[1], numArrCopy[2], numArrCopy[3] = numArrCopy[2], numArrCopy[3], numArrCopy[0], numArrCopy[1]
					}

					str1 := strings.Split(strArr[i], "米")[len(strings.Split(strArr[i], "米"))-1]
					strResult := "南麂海域最大出现" + tools.Float64ToFixedStr(numArrCopy[2], 1) + "-" + tools.Float64ToFixedStr(numArrCopy[3], 1) + "米" + str1
					arr = append(arr, strResult)

					numArr1 := [4]float64{0, 0, 0, 0}
					numArr1[0] = tools.Float64ToFixed(numArr[1]*0.5, 1)
					if numArr1[0] > 1.5 {
						numArr1[1] = numArr1[0] + 0.4
					} else {
						numArr1[1] = numArr1[0] + 0.3
					}

					numArr1[2] = tools.Float64ToFixed(numArr[3]*0.5, 1)
					if numArr1[2] > 1.5 {
						numArr1[3] = numArr1[2] + 0.4
					} else {
						numArr1[3] = numArr1[2] + 0.3
					}

					var skArr []string
					//	根据字符串中出现时刻的先后顺序获取字符串中的所有时刻
					for _, word := range strings.Split(strArr[i], "，") {
						for _, v := range sk {
							if strings.Contains(word, v) {
								skArr = append(skArr, v)
							}
						}
					}
					str2 := strings.Split(strings.Split(strArr[i], "今天"+skArr[0])[1], tools.Float64ToFixedStr(numArr[0], 1))[0]
					str3 := ""
					if numArr[1] > numArr[3] {
						str3 = "减小到"
					} else {
						str3 = "增大到"
					}
					strWave1 := ""
					strWave2 := ""

					if getWaveGrade(numArr1[0]) == getWaveGrade(numArr1[1]) {
						strWave1 = getWaveGrade(numArr1[0])
					} else {
						strWave1 = strings.Split(getWaveGrade(numArr1[0]), "浪")[0] + "到" + getWaveGrade(numArr1[1])
					}

					if getWaveGrade(numArr1[2]) == getWaveGrade(numArr1[3]) {
						strWave2 = getWaveGrade(numArr1[2])
					} else {
						strWave2 = strings.Split(getWaveGrade(numArr1[2]), "浪")[0] + "到" + getWaveGrade(numArr1[3])
					}

					//str3 := strings.Split(strings.Split(strArr[i], "明天"+skArr[1])[1], tools.Float64ToFixedStr(numArr[2], 1))[1]
					strResult1 := regionCnMap[region] + "今天" + skArr[0] + str2 + tools.Float64ToFixedStr(numArr1[0], 1) + "-" + tools.Float64ToFixedStr(numArr1[1], 1) + "米的" + strWave1 + "，明天" + skArr[1] + str3 + tools.Float64ToFixedStr(numArr1[2], 1) + "-" + tools.Float64ToFixedStr(numArr1[3], 1) + "米的" + strWave2 + "。"
					arr = append(arr, strResult1)
				} else if len(numArr) == 2 {
					str1 := strings.Split(strArr[i], "米")[len(strings.Split(strArr[i], "米"))-1]
					strResult := "温州近岸海域最大出现" + tools.Float64ToFixedStr(numArr[0], 1) + "-" + tools.Float64ToFixedStr(numArr[1], 1) + "米" + str1
					arr = append(arr, strings.ReplaceAll(strResult, "；", "。"))

					numArr1 := [2]float64{0, 0}
					numArr1[0] = tools.Float64ToFixed(numArr[0]*0.5, 1)
					if numArr1[0] > 1.5 {
						numArr1[1] = numArr1[0] + 0.4
					} else {
						numArr1[1] = numArr1[0] + 0.3
					}

					var skArr []string
					//	根据字符串中出现时刻的先后顺序获取字符串中的所有时刻
					for _, word := range strings.Split(strArr[i], "，") {
						for _, v := range sk {
							if strings.Contains(word, v) {
								skArr = append(skArr, v)
							}
						}
					}

					if len(skArr) == 0 {
						continue
					} else {
						str2 := strings.Split(strings.Split(strArr[i], "今天"+skArr[0])[1], tools.Float64ToFixedStr(numArr[0], 1))[0]
						strWave1 := ""

						if getWaveGrade(numArr1[0]) == getWaveGrade(numArr1[1]) {
							strWave1 = getWaveGrade(numArr1[0])
						} else {
							strWave1 = strings.Split(getWaveGrade(numArr1[0]), "浪")[0] + "到" + getWaveGrade(numArr1[1])
						}

						strResult1 := regionMap[region] + "今天" + skArr[0] + str2 + tools.Float64ToFixedStr(numArr1[0], 1) + "-" + tools.Float64ToFixedStr(numArr1[1], 1) + "米" + strWave1
						arr = append(arr, strResult1+"。")
					}
				}
			}
			if strings.Contains(strArr[i], "请") {
				//strArr[i] = strings.Replace(strArr[i], "我台的", "", -1)
				arr = append(arr, strArr[i])
			}
		} else if region == "qt" || region == "lw" || region == "lg" || region == "lq" {
			if strings.Contains(strArr[i], "温州近岸") {
				strArr[i] = tools.DelColor(strArr[i])
				numArr := tools.GetNumFloat64(strArr[i])
				if len(numArr) == 4 {
					numArrCopy := make([]float64, len(numArr))
					copy(numArrCopy, numArr)
					if numArrCopy[1] > numArrCopy[3] {
						numArrCopy[0], numArrCopy[1], numArrCopy[2], numArrCopy[3] = numArrCopy[2], numArrCopy[3], numArrCopy[0], numArrCopy[1]
					}
					str1 := strings.Split(strArr[i], "米")[len(strings.Split(strArr[i], "米"))-1]
					strResult := "温州近岸海域最大出现" + tools.Float64ToFixedStr(numArrCopy[2], 1) + "-" + tools.Float64ToFixedStr(numArrCopy[3], 1) + "米" + str1
					//arr = append(arr, strings.ReplaceAll(strResult, "；", "。"))
					arr = append(arr, strResult)

					numArr1 := [4]float64{0, 0, 0, 0}
					numArr1[0] = numArr[0] * 0.5
					numArr1[1] = numArr[1] * 0.5
					numArr1[2] = numArr[2] * 0.5
					numArr1[3] = numArr[3] * 0.5

					var skArr []string
					//	根据字符串中出现时刻的先后顺序获取字符串中的所有时刻

					for _, word := range strings.Split(strArr[i], "，") {
						for _, v := range sk {
							if strings.Contains(word, v) {
								skArr = append(skArr, v)
							}
						}
					}
					str2 := strings.Split(strings.Split(strArr[i], "今天"+skArr[0])[1], tools.Float64ToFixedStr(numArr[0], 1))[0]
					str3 := ""
					if numArr[1] > numArr[3] {
						str3 = "减小到"
					} else {
						str3 = "增大到"
					}
					strWave1 := ""
					strWave2 := ""

					if getWaveGrade(numArr1[0]) == getWaveGrade(numArr1[1]) {
						strWave1 = getWaveGrade(numArr1[0])
					} else {
						strWave1 = strings.Split(getWaveGrade(numArr1[0]), "浪")[0] + "到" + getWaveGrade(numArr1[1])
					}

					if getWaveGrade(numArr1[2]) == getWaveGrade(numArr1[3]) {
						strWave2 = getWaveGrade(numArr1[2])
					} else {
						strWave2 = strings.Split(getWaveGrade(numArr1[2]), "浪")[0] + "到" + getWaveGrade(numArr1[3])
					}

					//str3 := strings.Split(strings.Split(strArr[i], "明天"+skArr[1])[1], tools.Float64ToFixedStr(numArr[2], 1))[1]
					strResult1 := regionMap[region] + "今天" + skArr[0] + str2 + tools.Float64ToFixedStr(numArr1[0], 1) + "-" + tools.Float64ToFixedStr(numArr1[1], 1) + "米的" + strWave1 + "，明天" + skArr[1] + str3 + tools.Float64ToFixedStr(numArr1[2], 1) + "-" + tools.Float64ToFixedStr(numArr1[3], 1) + "米的" + strWave2 + "。"
					arr = append(arr, strResult1)
				} else if len(numArr) == 2 {
					str1 := strings.Split(strArr[i], "米")[len(strings.Split(strArr[i], "米"))-1]
					strResult := "温州近岸海域最大出现" + tools.Float64ToFixedStr(numArr[0], 1) + "-" + tools.Float64ToFixedStr(numArr[1], 1) + "米" + str1
					arr = append(arr, strings.ReplaceAll(strResult, "；", "。"))

					numArr1 := [2]float64{0, 0}
					numArr1[0] = numArr[0] * 0.5
					numArr1[1] = numArr[1] * 0.5

					var skArr []string
					//	根据字符串中出现时刻的先后顺序获取字符串中的所有时刻
					for _, word := range strings.Split(strArr[i], "，") {
						for _, v := range sk {
							if strings.Contains(word, v) {
								skArr = append(skArr, v)
							}
						}
					}

					if len(skArr) == 0 {
						continue
					} else {
						str2 := strings.Split(strings.Split(strArr[i], "今天"+skArr[0])[1], tools.Float64ToFixedStr(numArr[0], 1))[0]
						strWave1 := ""

						if getWaveGrade(numArr1[0]) == getWaveGrade(numArr1[1]) {
							strWave1 = getWaveGrade(numArr1[0])
						} else {
							strWave1 = strings.Split(getWaveGrade(numArr1[0]), "浪")[0] + "到" + getWaveGrade(numArr1[1])
						}

						strResult1 := regionMap[region] + "今天" + skArr[0] + str2 + tools.Float64ToFixedStr(numArr1[0], 1) + "-" + tools.Float64ToFixedStr(numArr1[1], 1) + "米" + strWave1
						arr = append(arr, strResult1+"。")
					}
				}
			}
			if strings.Contains(strArr[i], "请") {
				//strArr[i] = strings.Replace(strArr[i], "我台的", "", -1)
				arr = append(arr, strArr[i])
			}
		} else if region == "hz" {
			if strings.Contains(strArr[i], "温州近海") {
				strArr[i] = strings.Split(strArr[i], "温州近海海域")[1]
				arr = append(arr, "近海海域："+strArr[i])
			}
			if strings.Contains(strArr[i], "温州近岸") {
				//strArr[i] = strings.Split(strArr[i], "温州近岸海域")[1]
				ja := strings.Split(strArr[i], "温州近岸海域")[1]
				ja1 := "洞头、瑞安、平阳、苍南" + ja
				ja2 := "龙湾、龙港" + ja
				ja3 := "乐清" + ja
				arr = append(arr, "近岸海域："+ja1+ja2+ja3)
			}
			if strings.Contains(strArr[i], "请") {
				arr = append(arr, strArr[i])
			}
		}
	}
	if region == "hz" {
		for i := 0; i < len(arr); i++ {
			for j := i + 1; j < len(arr); j++ {
				if strings.Contains(arr[i], "近岸海域") && strings.Contains(arr[j], "近海海域") {
					arr[i], arr[j] = arr[j], arr[i]
				}
			}
		}
	}
	alarmDetail.Data = strings.Join(arr, "\n")
	tools.SuccessResp(c, "获取成功", alarmDetail)
	return
}

// HandleWaveInfo
func HandleWaveInfo(alarmDetail *models.AlarmDetail, region string, db *gorm.DB, c *gin.Context) {
	str := alarmDetail.Data
	//去除字符串中的空格
	str = strings.Replace(str, " ", "", -1)
	//根据换行符分割
	strArr := strings.Split(str, "\n")
	for i := 0; i < len(strArr); i++ {
		if len(strArr[i]) == 0 {
			strArr = append(strArr[:i], strArr[i+1:]...)
			i--
		}
	}
	if region == "hz" {
		alarmDetail.Data = strings.Join(strArr, "\n")
		tools.SuccessResp(c, "获取成功", alarmDetail)
	}
	var arr []string
	for i := 0; i < len(strArr); i++ {
		if strings.Contains(strArr[i], "根据") {
			arr = append(arr, strArr[i])
		}
		if strings.Contains(strArr[i], "未来24小时") {
			strArr[i] = strings.Split(strArr[i], "，")[0] + "，" + regionMap[region] + "未来24小时海浪预报："
			arr = append(arr, strArr[i])
		}
		if strings.Contains(strArr[i], "近岸海域") && strings.Contains(strArr[i], "近海海域") {
			itemArr := strings.Split(strArr[i], "，")
			for j := 0; j < len(itemArr); j++ {
				if strings.Contains(itemArr[j], "近海海域") {
					itemArr = append(itemArr[:j], itemArr[j+1:]...)
					j--
				}
			}
			strArr[i] = strings.Join(itemArr, "，")
			arr = append(arr, strArr[i])
		}
	}
	alarmDetail.Data = strings.Join(arr, "\n")
	tools.SuccessResp(c, "获取成功", alarmDetail)
	return
}

// HandleWaveJiechu
func HandleWaveJiechu(alarmDetail *models.AlarmDetail, region string, db *gorm.DB, c *gin.Context) {
	str := alarmDetail.Data
	//去除字符串中的空格
	str = strings.Replace(str, " ", "", -1)
	str = strings.Replace(str, "温州", regionCnMap[region], -1)
	//根据换行符分割
	strArr := strings.Split(str, "\n")
	for i := 0; i < len(strArr); i++ {
		if len(strArr[i]) == 0 {
			strArr = append(strArr[:i], strArr[i+1:]...)
			i--
		}
	}
	alarmDetail.Data = strings.Join(strArr, "\n")
	tools.SuccessResp(c, "获取成功", alarmDetail)
}
